<?php
class Merchant_Service_WarehouseProducts
{

    public static function add($row){
        $obj = new Merchant_Model_WarehouseProducts();
        return $obj->add($row);
	}

	public static function update($row,$value,$field='st_id'){
        $obj = new Merchant_Model_WarehouseProducts();
        return $obj->update($row,$value,$field);
	}


	public static function delete($value,$field='st_id'){
        $obj = new Merchant_Model_WarehouseProducts();
        return $obj->delete($value,$field);

	}


	public static function getByValue($value,$field='st_id'){
        $obj = new Merchant_Model_WarehouseProducts();
        return $obj->getByValue($value,$field);
	}
	
	public static function get($id,$customerId = null){
		$wp = self::getByValue($id,"wp_id");
		if(is_array($wp) && !empty($wp)){
			$productService = new Merchant_Service_Product();
			$product = $productService->getProductInfo($wp['product_id'],$customerId);
			if(is_array($product) && !empty($product)){
				$wp['product'] = $product;
				return $wp;
			}
			return false;
		}
		return false;
	}

	public static function getByProductId($productId,$customerId=null){
		$wps = self::getByCondition(array("product_id"=> $productId));
		if(is_array($wps) && !empty($wps)){
			$productService = new Merchant_Service_Product();
			foreach($wps as $key=>$value){
				$product = $productService->getProductInfo($value['product_id'],$customerId);
				if(is_array($product) && !empty($product)){
					$wps[$key]['product'] = $product;
				}else{
					unset($wps[$key]);
				}
			}
			
			return empty($wps)?false:$wps;
		}
		return false;
	}
	public static function getStockByProduct($productId){
		$condition['product_id'] = $productId;
		$whObj = new Merchant_Model_ProductStocket();
		$whData = $whObj->getByCondition($condition);
		
		$wh = new Merchant_Model_Warehouse(); 
		$stock = array();
		 foreach($whData as  $value){
			$whInfo = $wh->getByValue($value['warehouse_id']);
			$stock[$value['warehouse_id']]['warehouse'] = $whInfo['warehouse_name'];
			$stock[$value['warehouse_id']]['stock'] = $value['ps_quantity'];
			$stock[$value['warehouse_id']]['stockOnHold'] = $value['ps_quantity_hold'];
		}
		return empty($stock)?false:$stock;
		 
	}
	
	
    public static function getByCondition($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $obj = new Merchant_Model_WarehouseProducts();
        return $obj->getByCondition($condition,$type,$orderby,$page,$pageSize);
	}

    public static function getByConditionJoin($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $obj = new Merchant_Model_WarehouseProducts();
        return $obj->getByConditionJoin($condition,$type,$orderby,$page,$pageSize);
	}


    public static function getInfo($rowList) {

        foreach($rowList as $key =>$row){
            $picture=Merchant_Service_ProductAttached::getProductPicture($row['product_id']);
            $productObj = Merchant_Service_Product::getInstance();
            $product = Merchant_Service_Product::getInstance()->getProductInfo($row['product_id']);
            $statusList = $productObj->statusText;
            $rowList[$key]['product_status_name'] = @$statusList[$row['product_status']];
            $rowList[$key]['poc_value'] = $product['category']['poc_value'];
            if(isset($picture['img']))
            {
                $rowList[$key]['img_path'] = $picture['pa_path'];
            }
        }
        return $rowList;
    }
	
	
	/*
	*
	*产品下架
	*
	*/
	public static function pickingProcdut($productId,$quantity,$warehouseId='1',$skuName=null){
		$obj = new Merchant_Model_WarehouseProducts();
		$rowProduct = $obj->lockForUpdate($productId,$warehouseId);
		if($quantity <= $rowProduct['wp_quantity']){
			$currentStock = $rowProduct['wp_quantity']-$quantity;
			$currentHold = $rowProduct['wp_quantity_hold']+$quantity;
			$timeNow = date('Y-m-d H:i:s');
			$whProduct =	array('wp_quantity'=>$currentStock ,
								'wp_quantity_hold'=>$currentHold,
								'wh_last_update'=>$timeNow);
			if($skuName) $note = 'packing product:'.$skuName.' quantity '.$quantity;
			$note = 'packing product:'.$productId.' quantity '.$quantity;
			$wslRow = array('ws_code'=>$rowProduct['ws_code'],
							'wsl_current_val'=>$currentStock,
							'wsl_current_hold'=>$currentHold,
							'wsl_type'=>1,
							'user_id'=>'-1',
							'wsl_note'=>$note,
							'wsl_create_date'=>$timeNow,
							'wsl_ip'=>Default_Service_Common::getIP()
							);
							
			$slObj = new Merchant_Model_WarehouseShelfLog();
			$obj->update($whProduct,$rowProduct['wp_id']);
			$slObj->add($wslRow);
			return true;
		}
		return false;
	}
	
 
}